//
//  ReviewsVC.m
//  DFront
//
//  Created by lanou on 15/10/30.
//  Copyright © 2015年 xuzenghui. All rights reserved.
//

#import "ReviewsVC.h"
#import "PKRequestManager.h"
#import "ReviewsModel.h"
#import "ReviewsCell.h"
#import "PKRequestManager.h"
#import "DFClickAppleViewController.h"
#import "MJRefresh.h"

@interface ReviewsVC ()

@property(nonatomic,strong)NSMutableArray *DataSourceArray;
@property(nonatomic,strong)ReviewsModel *model;
@property(nonatomic,assign)int page;
@end

@implementation ReviewsVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _DataSourceArray = [NSMutableArray array];
    
    [self.tableView registerClass:[ReviewsCell class] forCellReuseIdentifier:@"cell"];
    self.tableView.rowHeight = 76;
    [self requestDataWithType:0];
    
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self requestDataWithType:0];
    }];
    self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self requestDataWithType:1];
    }];
}

-(void)requestDataWithType:(NSInteger)type
{
    if (type == 0) {
        _page = 1;
    }
    else
    {
        _page ++;
    }
    NSString *str = [NSString stringWithFormat:@"http://lib.wap.zol.com.cn/ipj/docList/?v=4.0&class_id=2&isReviewing=NO&last_time=2015-10-30%2015%3A58&page=%d&retina=1&vs=iph440",_page];
    
    [PKRequestManager requestWithType:GET urlString:str parDic:nil finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers |NSJSONReadingMutableLeaves error:nil];
        if (type == 0) {
            [self.DataSourceArray removeAllObjects];
        }
        NSArray *listArray = dic[@"list"];
        for (NSDictionary *modelDic in listArray) {
            _model = [[ReviewsModel alloc]init];

            if (![modelDic isKindOfClass:[NSNull class]]) {
            [_model setValuesForKeysWithDictionary:modelDic];
            }

        
            

            [_DataSourceArray addObject:_model];
        }
        [self.tableView.header endRefreshing];
        [self.tableView.footer endRefreshing];
        [self.tableView reloadData];
        
    } error:^(NSError *error) {
        [self.tableView.header endRefreshing];
        [self.tableView.footer endRefreshing];
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return _DataSourceArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ReviewsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.ConfigureModel = _DataSourceArray[indexPath.row];
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ReviewsModel *model = _DataSourceArray[indexPath.row];
    DFClickAppleViewController *reviewWeb = [[DFClickAppleViewController alloc] init];
    reviewWeb.str = model.url;
    [self.navigationController pushViewController:reviewWeb animated:YES];
}

@end
